package com.enation.app.web.tag;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.enation.app.web.core.model.Member;
import com.enation.app.web.core.model.SpiteVisitor;
import com.enation.app.web.core.service.ISpiteVisitorManager;
import com.enation.app.web.core.service.impl.MemberManager;
import com.enation.framework.context.webcontext.ThreadContextHolder;
import com.enation.framework.taglib.BaseFreeMarkerTag;
import com.enation.framework.util.ip.RealIp;

import freemarker.template.TemplateModelException;

/**
 * 
 * @function 恶意访问日志
 * @author 闯哥
 * @time 2016年12月10日 下午11:44:39
 */
@Component
public class SpiteLogTag extends BaseFreeMarkerTag {

	@Autowired
	private MemberManager memberManager;

	@Autowired
	private ISpiteVisitorManager spiteVisitorManager;

	@Override
	protected Object exec(Map params) throws TemplateModelException {

		String visitPage = params.get("visitPage").toString();
		String pageName = params.get("pageName").toString();
		String flag = params.get("flag").toString();

		Member member = this.memberManager.getCurrentLogin();

		// 注册页面、登录页面
		if (visitPage.equals("register.html") || visitPage.equals("login.html")
				|| flag.equals(0)) {
			if (member == null) {
				return 1;
			}
		}
		// 个人中心页面
		if (visitPage.equals("member.html") || flag.equals("1")) {
			if (member != null) {
				return 1;
			}
		}

		SpiteVisitor spiteVisitor = new SpiteVisitor();

		if (member != null) {
			spiteVisitor.setMember_id(member.getMember_id());
			spiteVisitor.setStudent_number(member.getStudent_number());
			spiteVisitor.setReal_name(member.getReal_name());
		}

		spiteVisitor.setVisitPage(visitPage);
		spiteVisitor.setPageName(pageName);
		String ip = RealIp.getIpAddress(ThreadContextHolder.getHttpRequest());
		spiteVisitor.setIp(ip);
		spiteVisitor.setTime(new Date().getTime() / 1000);

		try {
			this.spiteVisitorManager.insert(spiteVisitor);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return 0;
	}

}
